package aufgabe3_chat;

import java.io.IOException;

import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JOptionPane;

import aufgabe3_chat.chatGui.ChatFenster;
import aufgabe3_chat.chatGui.Userliste;

public class Client_TCP extends Thread {

	private Chat_User_Administration chat_user;
	private boolean client_run;
	private Logger logger;
	private Userliste userliste;
	private String ip;
	private int port;
	private String username;
	private final int WAITTIME = 5000;
	
	public Client_TCP(Chat_User_Administration chat_user, Logger logger, Userliste userliste, int port){	
		this.chat_user = chat_user;
		this.logger = logger;
		this.client_run = true;
		this.userliste = userliste;
		this.port = port;
	}
	
	public void run(){
		Socket server = null;
		
		try {
			
			// Verbindung zum Server via TCP herstellen auf Port 50000
			logger.log("Client_TCP",  "Socket Coneckt", "Habe Port, versuche Socket zu erstelln!");
			server = new Socket(ip, port);
			logger.log("Client_TCP", "Socket Coneckt", "Habe Port, versuche Socket zu oeffnen!");
			logger.log("Client_TCP", "Connect zum Server", "Begin");
			Scanner in = new Scanner(server.getInputStream());
			PrintWriter out = new PrintWriter(server.getOutputStream(), true);
			logger.log("Client_TCP", "Connect zum Server", "Geschaft");
			
			logger.log("Client_TCP", "Try Username Change", "Username set to: " + username);
			out.println("NEW " + username);
			String server_answer = in.nextLine();
			
			if(server_answer.equals("OK")){
				logger.log("Client_TCP", "Username Change", "Username set to: " + username);
			} else {
				logger.log("Client_TCP", "Disconect ERROR", server_answer);
				client_run = false;
			}
			
			while(client_run){
				
				out.println("INFO");
				server_answer = in.nextLine();
				
				if(server_answer.startsWith("ERROR")){
					server.close();
					logger.log("Client_TCP", "Disconect ERROR", server_answer);
					return;
				}
				
				String[] splitInput = server_answer.split(" ", 3);

				Chat_User_Administration chat_user_new = new Chat_User_Administration(logger);

					int times = new Integer(splitInput[1]);
				
					for(int i = 0; i < times; i++){
						splitInput = splitInput[2].split(" " , 3);
						chat_user_new.add(splitInput[0], splitInput[1]);
					}			
			
					chat_user = chat_user_new;
					userliste.makeListe(chat_user);
					
					my_sleep(WAITTIME);

			}
			out.println("BYE");
			logger.log("Client_TCP", "BYE Disconect", in.nextLine());
			server.close();
		} catch (UnknownHostException e) {
			logger.log("Client_TCP", "Exception", "UnknownHost");
			JOptionPane.showMessageDialog(null, "Host ist unbekannt!", "Unbekannter Host", JOptionPane.ERROR_MESSAGE, null);
		} catch (IOException e) {
			logger.log("Client_TCP", "Exception", "IO");
			JOptionPane.showMessageDialog(null, "Host ist unbekannt!", "Unbekannter Host", JOptionPane.ERROR_MESSAGE, null);
		}
		finally {
			try {
				server.close();
				logger.log("Client_TCP", "Beenden","Console: Client Socket disconnect");
			} catch (IOException e) {
			} catch (NullPointerException e){
				
			}
		}
		
	}
	
	
	public void disconect(ChatFenster chatfenster){
		client_run = false;
	}
	
	public void set_user_name(String name){
		logger.log("Client_TCP", "Change Username", "Get New Name");
		username = name;
	}
	
	public void close(){
		client_run = false;
	}
	
	public String getUserName(){
		return username;
	}
	
	public void set_server_ip(String ip) {
		logger.log("Client_TCP", "Change Username", "Get New Name");
		this.ip = ip;
	}
	
	public void my_sleep(int time){
		try {
			sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


}
